<?php

// @author Wang Yanxiang
// Changes are made based on the input element

require_once('HTML/reCAPTCHA/recaptchalib.php');
require_once("HTML/QuickForm/element.php");

class HTML_QuickForm_recaptcha extends HTML_QuickForm_element
{
    // Constructor for reCAPTCHA element in HTML
    function HTML_QuickForm_recaptcha($elementName=null, $elementLabel=null, $attributes=null)
    {
        $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
        $this->_publickey = "6LflQ-4SAAAAACjgP8SLY2ViZc7-YPTZfr7q0d7T";
    } //end constructor

    function setType($type)
    {
        $this->_type = $type;
        $this->updateAttributes(array('type'=>$type));
    } 

    function setName($name)
    {
        $this->updateAttributes(array('name'=>$name));
    }

    function getName()
    {
        return $this->getAttribute('name');
    } 

    function setValue($value)
    {
        $this->updateAttributes(array('value'=>$value));
    }

    function getValue()
    {
        return $this->getAttribute('value');
    } 

    // return the HTML using the reCAPTCHA library function call
    function toHtml()
    {
        return $this->_getTabs() . "<script type='text/javascript'> var RecaptchaOptions = { theme : 'clean' }; </script>" . recaptcha_get_html($this->_publickey, null, true);
        // if ($this->_flagFrozen) {
        //     return $this->getFrozenHtml();
        // } else {
        //     return $this->_getTabs() . recaptcha_get_html($this->_publickey);
        // }
    }

    function onQuickFormEvent($event, $arg, &$caller)
    {
        // do not use submit values for button-type elements
        $type = $this->getType();
        if (('updateValue' != $event) ||
            ('submit' != $type && 'reset' != $type && 'image' != $type && 'button' != $type)) {
            parent::onQuickFormEvent($event, $arg, $caller);
        } else {
            $value = $this->_findValue($caller->_constantValues);
            if (null === $value) {
                $value = $this->_findValue($caller->_defaultValues);
            }
            if (null !== $value) {
                $this->setValue($value);
            }
        }
        return true;
    } 
    
    function exportValue(&$submitValues, $assoc = false)
    {
        $type = $this->getType();
        if ('reset' == $type || 'image' == $type || 'button' == $type || 'file' == $type) {
            return null;
        } else {
            return parent::exportValue($submitValues, $assoc);
        }
    }
    
} // end class HTML_QuickForm_element
?>
